package com.zb.zeus.system.api.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.zb.zeus.common.core.annotation.Excel;
import com.zb.zeus.common.core.constant.UserConstants;
import com.zb.zeus.common.core.web.domain.BaseEntity;
import com.zb.zeus.system.api.domain.dto.RoleMenuPower;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 角色表 t_sys_role
 *
 * @author zb.zeus
 */
@Data
@TableName(value = "t_sys_role")
@ApiModel(value = "角色实体", description = "角色实体")
public class SysRole extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 系统
     */
    @Excel(name = "所属系统id")
    @ApiModelProperty(value = "所属系统id")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long systemId;

    /**
     * 系统
     */
    @Excel(name = "所属系统编码")
    @ApiModelProperty(value = "所属系统编码")
    private String systemCode;

    /**
     * 系统
     */
    @Excel(name = "所属系统名称")
    @ApiModelProperty(value = "所属系统名称")
    private String systemName;

    /**
     * 角色名称
     */
    @Excel(name = "角色名称")
    @ApiModelProperty(value = "角色名称")
    private String roleName;

    /**
     * 角色权限
     */
    @Excel(name = "角色权限")
    @ApiModelProperty(value = "角色权限")
    private String roleKey;

    /**
     * 角色排序
     */
    @Excel(name = "角色排序")
    @ApiModelProperty(value = "角色排序")
    private String roleSort;

    /**
     * 数据范围（1：所有数据权限；2：自定义数据权限；3：本组织数据权限；4：本组织及以下数据权限；5：仅本人数据权限）(数据权限已改造，字段已废弃)
     */
    @Excel(name = "数据范围", readConverterExp = "1=所有数据权限,2=自定义数据权限,3=本组织数据权限,4=本组织及以下数据权限,5=仅本人数据权限")
    @ApiModelProperty(value = "数据范围（1：所有数据权限；2：自定义数据权限；3：本组织数据权限；4：本组织及以下数据权限；5：仅本人数据权限）(数据权限已改造，字段已废弃)")
    private String dataScope;

    /**
     * 菜单树选择项是否关联显示（ 0：父子不互相关联显示 1：父子互相关联显示）
     */
    @ApiModelProperty(value = "菜单树选择项是否关联显示（ 0：父子不互相关联显示 1：父子互相关联显示）")
    private boolean menuCheckStrictly;

    /**
     * 组织树选择项是否关联显示（0：父子不互相关联显示 1：父子互相关联显示 ）
     */
    @ApiModelProperty(value = "组织树选择项是否关联显示（0：父子不互相关联显示 1：父子互相关联显示 ）")
    private boolean orgCheckStrictly;

    /**
     * 角色状态（0正常 1停用）
     */
    @Excel(name = "角色状态", readConverterExp = "0=正常,1=停用")
    @ApiModelProperty(value = "角色状态（0正常 1停用）")
    private String status;

    /**
     * 备注
     */
    @Excel(name = "备注")
    @ApiModelProperty(value = "备注")
    private String remark;


    /**
     * 用户是否存在此角色标识 默认不存在
     */
    @ApiModelProperty(value = "用户是否存在此角色标识 默认不存在")
    @TableField(exist = false)
    private boolean flag = false;

    /**
     * 菜单组
     */
    @ApiModelProperty(value = "菜单组")
    @TableField(exist = false)
    private Long[] menuIds;

    /**
     * 角色菜单权限组
     */
    @ApiModelProperty(value = "角色菜单权限组")
    @TableField(exist = false)
    private List<RoleMenuPower> roleMenuPowerList;

    /**
     * 组织组（数据权限）
     */
    @ApiModelProperty(value = "组织组（数据权限）")
    @TableField(exist = false)
    private Long[] orgIds;


    public SysRole() {

    }

    public SysRole(Long id) {
        this.setId(id);
    }


    public boolean isAdmin() {
        return isAdmin(this.getId());
    }

    public static boolean isAdmin(Long id) {
        return UserConstants.ADMIN_ROLE_ID.equals(id);
    }

    @NotBlank(message = "角色名称不能为空")
    @Size(min = 0, max = 30, message = "角色名称长度不能超过30个字符")
    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @NotBlank(message = "权限字符不能为空")
    @Size(min = 0, max = 100, message = "权限字符长度不能超过100个字符")
    public String getRoleKey() {
        return roleKey;
    }

    public void setRoleKey(String roleKey) {
        this.roleKey = roleKey;
    }

    @NotBlank(message = "显示顺序不能为空")
    public String getRoleSort() {
        return roleSort;
    }

    public void setRoleSort(String roleSort) {
        this.roleSort = roleSort;
    }

    public String getDataScope() {
        return dataScope;
    }

    public void setDataScope(String dataScope) {
        this.dataScope = dataScope;
    }

    public boolean isMenuCheckStrictly() {
        return menuCheckStrictly;
    }

    public void setMenuCheckStrictly(boolean menuCheckStrictly) {
        this.menuCheckStrictly = menuCheckStrictly;
    }

    public boolean isOrgCheckStrictly() {
        return orgCheckStrictly;
    }

    public void setOrgCheckStrictly(boolean orgCheckStrictly) {
        this.orgCheckStrictly = orgCheckStrictly;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Long[] getMenuIds() {
        return menuIds;
    }

    public void setMenuIds(Long[] menuIds) {
        this.menuIds = menuIds;
    }

    public Long[] getOrgIds() {
        return orgIds;
    }

    public void setOrgIds(Long[] orgIds) {
        this.orgIds = orgIds;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("roleName", getRoleName())
                .append("roleKey", getRoleKey())
                .append("roleSort", getRoleSort())
                .append("dataScope", getDataScope())
                .append("menuCheckStrictly", isMenuCheckStrictly())
                .append("orgCheckStrictly", isOrgCheckStrictly())
                .append("status", getStatus())
                .append("createBy", getCreateBy())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .toString();
    }
}
